#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

#define S_IT set<Node>::iterator
typedef long long ll;

struct Node {
  int l, r;
  mutable ll val;
  bool operator<(const Node &a) const { return l < a.l; }
  Node(int a, int b, ll v);
  Node(int a);
};

S_IT split(int pos);
void add(int l, int r, int v);
ll kth(int l, int r, int k);
ll qpow(ll a, int b, ll y);
ll query(int l, int r, int x, ll y);
void assign(int l, int r, int v);
int rnd(void);

set<Node> s;
ll seed;
int n, m, vmax;

int main(void) {
  ios::sync_with_stdio(false);
  cin >> n >> m >> seed >> vmax;
  for (int i = 1; i <= n; ++i) {
    static int temp = 0;
    temp = rnd() % vmax + 1;
    s.insert(Node(i, i, (ll)temp));
  }
  s.insert(Node(n + 1, n + 1, 0));
  for (int i = 1; i <= m; ++i) {
    static int l = 0, r = 0, x = 0, y = 0, op = 0;
    op = rnd() % 4 + 1;
    l = rnd() % n + 1, r = rnd() % n + 1;
    if (l > r) {
      swap(l, r);
    }
    if (op == 3) {
      x = rnd() % (r - l + 1) + 1;
    } else {
      x = rnd() % vmax + 1;
    }
    if (op == 4) {
      y = rnd() % vmax + 1;
    }

    if (op == 1) {
      add(l, r, (ll)x);
    } else if (op == 2) {
      assign(l, r, (ll)x);
    } else if (op == 3) {
      cout << kth(l, r, x) << endl;
    } else if (op == 4) {
      cout << query(l, r, x, (ll)y) << endl;
    }
  }
  // system("pause");
  return 0;
}

Node::Node(int a, int b, ll v) {
  l = a;
  r = b;
  val = v;
}

Node::Node(int a) { l = a; }

S_IT split(int pos) {
  S_IT it = s.lower_bound(Node(pos));
  if (it != s.end() && it->l == pos) {
    return it;
  }
  --it;
  int l = it->l, r = it->r;
  ll val = it->val;
  s.erase(it);
  s.insert(Node(l, pos - 1, val));
  return s.insert(Node(pos, r, val)).first;
}

void add(int l, int r, int v) {
  S_IT it2 = split(r + 1), it1 = split(l);
  for (S_IT it = it1; it != it2; ++it) {
    it->val += v;
  }
}

ll kth(int l, int r, int k) {
  S_IT it2 = split(r + 1), it1 = split(l);
  vector<pair<ll, int> > arr;
  arr.clear();
  for (S_IT it = it1; it != it2; ++it) {
    arr.push_back(pair<ll, int>(it->val, it->r - it->l + 1));
  }
  sort(arr.begin(), arr.end());
  for (unsigned int i = 0; i < arr.size(); ++i) {
    k -= arr[i].second;
    if (k <= 0) {
      return arr[i].first;
    }
  }
}

ll qpow(ll a, int x, ll y) {
  ll b = 1LL;
  a %= y;
  while (x) {
    if (x & 1) {
      b = (b * a) % y;
    }
    a = (a * a) % y;
    x >>= 1;
  }
  return b;
}

ll query(int l, int r, int x, ll y) {
  S_IT it2 = split(r + 1), it1 = split(l);
  ll res = 0;
  for (S_IT it = it1; it != it2; ++it) {
    res = (res + (it->r - it->l + 1) * qpow(it->val, x, y)) % y;
  }
  return res;
}

void assign(int l, int r, int v) {
  S_IT it2 = split(r + 1), it1 = split(l);
  s.erase(it1, it2);
  s.insert(Node(l, r, v));
}

int rnd(void) {
  int ret = (int)seed;
  seed = (seed * 7 + 13) % 1000000007;
  return ret;
}
